package com.example.compreshensiveshop.service.imp;

import com.example.compreshensiveshop.dao.CartDao;
import com.example.compreshensiveshop.dao.SkuDao;
import com.example.compreshensiveshop.dto.CartAddDTO;
import com.example.compreshensiveshop.dto.CartUpdateDTO;
import com.example.compreshensiveshop.po.CartPO;
import com.example.compreshensiveshop.po.SkuPO;
import com.example.compreshensiveshop.service.CartService;
import com.example.compreshensiveshop.vo.CartListVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Cart Service Implementation
 */
@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartDao cartDao;
    @Autowired
    private SkuDao skuDao;
    @Override
    public void addCart(CartAddDTO cartAddDTO) {
        CartPO cartPO = CartPO.builder()
                .customerId(cartAddDTO.getCustomerId())
                .skuId(cartAddDTO.getSkuId())
                .quantity(cartAddDTO.getQuantity())
                .build();
        cartDao.addCart(cartPO);
    }

    @Override
    public PageInfo<CartListVO> queryByCId(Long customerId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<CartListVO> cartListVOS = cartDao.queryByCId(customerId);
        return PageInfo.of(cartListVOS);
    }

    @Override
    public void deleteByCIds(List<Long> ids) {
        cartDao.deleteByCIds(ids);
    }

    @Override
    public void updateCart(CartUpdateDTO cartUpdateDTO) {
        CartPO cartPO = CartPO.builder()
                .id(cartUpdateDTO.getId())
                .skuId(cartUpdateDTO.getSkuId())
                .quantity(cartUpdateDTO.getQuantity())
                .build();
        cartDao.updateCart(cartPO);
    }
}